
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:ycbl/config/bl_color.dart';
import 'package:ycbl/config/bl_style.dart';
import 'package:ycbl/widget/bl_border_button.dart';
import 'package:ycbl/widget/image.dart';

/// 一些状态相关的 Widget ，
/// 比如未登录显示的 Widget，加载中 Widget， 空页面 Widget， 页面错误

/// 基础 Widget
class ViewStateWidget extends StatelessWidget {

  /// 出现状况时显示的图片
  final Widget image;
  /// 出现状况时显示的提示文字
  final String message;
  /// 按钮提示文字
  final String buttonText;
  /// 按钮点击事件
  final VoidCallback onButtonPressed;
  /// 提示文字上间距
  final double messageTop;
  /// 提示文字下间距
  final double messageBottom;

  const ViewStateWidget({Key key, this.image, this.message, this.buttonText, this.onButtonPressed, this.messageTop = 20, this.messageBottom = 20}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        //居中显示
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          //图片提示
          image ?? Container(width: 0, height: 0,),
          //文字提示
          Padding(
            padding: EdgeInsets.only(top: messageTop, bottom: messageBottom,),
            child: Text(
              message ?? '',
              style: TextStyle(
                color: BLColor.commonGreyTextColor,
                fontSize: 14,
              ),
            ),
          ),

          //按钮
          buttonText == null ?
          Container(width: 0, height: 0,) :
          BLBorderButton(
            child: Text(buttonText, style: BLStyle.tabSelectTextStyle),
            onPressed: onButtonPressed,
          ),

        ],
      ),
    );
  }
}


/// 未登录
class ViewStateUnLoginWidget extends StatelessWidget {

  /// 出现状况时显示的图片
  final Widget image;
  /// 出现状况时显示的提示文字
  final String message;
  /// 按钮提示文字
  final String buttonText;
  /// 按钮点击事件
  final VoidCallback onButtonPressed;
  /// 提示文字上间距
  final double messageTop;
  /// 提示文字下间距
  final double messageBottom;

  const ViewStateUnLoginWidget({Key key, this.image, this.message, this.buttonText, this.onButtonPressed, this.messageTop = 20, this.messageBottom = 20}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ViewStateWidget(
      image: image,
      message: message ?? '登录后才能开始学习哦~~ ',
      buttonText: buttonText ?? '立即登录',
      onButtonPressed: onButtonPressed,
      messageTop: messageTop,
      messageBottom: messageBottom,
    );
  }
}

/// 空页面 404
class ViewStateEmptyWidget extends StatelessWidget {

  /// 出现状况时显示的图片
  final Widget image;
  /// 出现状况时显示的提示文字
  final String message;
  /// 按钮提示文字
  final String buttonText;
  /// 按钮点击事件
  final VoidCallback onButtonPressed;
  /// 提示文字上间距
  final double messageTop;
  /// 提示文字下间距
  final double messageBottom;

  const ViewStateEmptyWidget({Key key, this.image, this.message, this.buttonText, this.onButtonPressed, this.messageTop = 20, this.messageBottom = 20}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ViewStateWidget(
      image: image ?? LocalImage('pic_load_empty.png', width: 120,),
      message: message ?? '抱歉，没有找到相关内容',
      buttonText: buttonText,
      onButtonPressed: onButtonPressed,
    );
  }
}

/// 加载失败
class ViewStateErrorWidget extends StatelessWidget {

  /// 出现状况时显示的图片
  final Widget image;
  /// 出现状况时显示的提示文字
  final String message;
  /// 按钮提示文字
  final Widget buttonText;
  /// 按钮点击事件
  final VoidCallback onButtonPressed;
  /// 提示文字上间距
  final double messageTop;
  /// 提示文字下间距
  final double messageBottom;

  const ViewStateErrorWidget({Key key, this.image, this.message, this.buttonText, this.onButtonPressed, this.messageTop = 20, this.messageBottom = 20}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ViewStateWidget(
      image: image ?? LocalImage('pic_load_error.png', width: 250,),
      message: message ?? '加载失败，请稍后再试',
      buttonText: buttonText ?? '重新加载',
      onButtonPressed: onButtonPressed,
      messageTop: image == null ? 0 : messageTop,
    );
  }
}

/// 加载中
class ViewStateBusyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: LocalImage('pic_loading.gif', width: 150, height: 150,),
    );
  }
}

